package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.pojo.User;
import com.itheima.pojo.UserInfo;
import com.itheima.service.UserService;
import com.itheima.vo.RecommendationVo;
import com.itheima.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 王同学
 */
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {
    //Nacos动态端口测试
    @Value("${server.port}")
    private Integer port;
    @GetMapping("test")
    public String test(){

        System.out.println(port);
        return port.toString();
    }
  @Autowired
  private UserService userService;

    //获取短信登录
    @PostMapping("/sendMsg")
    public CommonResult sendMsg(@RequestBody User user){
            //把用户传进来的手机号码，拿去生成发送验证码
        boolean res = userService.sendMsg(user.getPhone());

        return CommonResult.success(res? 200:500,res? "验证码发送成功~！":"验证码发送失败",null);
    }

    @PostMapping("/login")
    public CommonResult login(@RequestBody User user){
        //登录模块
        if (user==null|| StringUtils.isEmpty(user.getPhone())||StringUtils.isEmpty(user.getCode())){
            return CommonResult.error("传入的参数不能为空~！");
        }
        UserVO userVO = userService.login(user);
                //给前端返回的是一个token
        return CommonResult.success(200,"登录成功~！！",userVO);
    }
    @PutMapping("/inof")
    public CommonResult inof(@RequestBody UserInfo userInfo){
        //如果是新用户的话，那么就跳转到这个页面来，把用户输入的信息，给填写进去~！
         boolean res=userService.inof(userInfo);

        return CommonResult.success(res);
    }

    @GetMapping("/recommendation")
    public CommonResult recommendation(@RequestParam double longitude,
                                       @RequestParam double latitude,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pageSize ){
            //传进来经纬度

        return userService.getRecommendationList(longitude, latitude,page,pageSize);
    }
    //修改手机号功能，在上面发送短信接口执行完毕之后，调用这个接口
    @PutMapping("/phone")
    public CommonResult updatePhone(@RequestBody User user){
        //修改手机号
        boolean res = userService.updateByPhone(user.getPhone());

        return CommonResult.success(res? "手机号修改成功":"手机号修改失败");
    }
    //修改用户资料
    @PutMapping("/redact")
    public CommonResult updateUser(@RequestBody User user){
        //一共传进来9个参数，都是非必填的
        boolean res = userService.updateUserById(user);
        if (!res){
            throw new RuntimeException("用户更新资料失败~！！");
        }

        return CommonResult.success(res);
    }
}
